package com.google.android.exoplayer2.audio;

import android.content.Context;
import android.media.AudioFocusRequest;
import android.media.AudioFocusRequest.Builder;
import android.media.AudioManager;
import android.media.AudioManager.OnAudioFocusChangeListener;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import com.huawei.hwphy2d.physical.util.BarrierParameters;
import o.aby;
import o.amm;
import o.amz;
import o.anr;

public final class AudioFocusManager {
    private float ʼ = 1.0f;
    private AudioFocusRequest ʽ;
    @Nullable
    private final AudioManager ˊ;
    private final b ˋ;
    private int ˎ;
    @Nullable
    private aby ˏ;
    private final a ॱ;
    private boolean ॱॱ;
    private int ᐝ;

    class a implements OnAudioFocusChangeListener {
        final /* synthetic */ AudioFocusManager ˊ;

        private a(AudioFocusManager audioFocusManager) {
            this.ˊ = audioFocusManager;
        }

        public void onAudioFocusChange(int i) {
            switch (i) {
                case -3:
                    if (!this.ˊ.ॱॱ()) {
                        this.ˊ.ˎ = 3;
                        break;
                    } else {
                        this.ˊ.ˎ = 2;
                        break;
                    }
                case -2:
                    this.ˊ.ˎ = 2;
                    break;
                case -1:
                    this.ˊ.ˎ = -1;
                    break;
                case 1:
                    this.ˊ.ˎ = 1;
                    break;
                default:
                    amz.ˏ("AudioFocusManager", "Unknown focus change type: " + i);
                    return;
            }
            switch (this.ˊ.ˎ) {
                case -1:
                    this.ˊ.ˋ.ˏ(-1);
                    this.ˊ.ˏ(true);
                    break;
                case 0:
                case 3:
                    break;
                case 1:
                    this.ˊ.ˋ.ˏ(1);
                    break;
                case 2:
                    this.ˊ.ˋ.ˏ(0);
                    break;
                default:
                    throw new IllegalStateException("Unknown audio focus state: " + this.ˊ.ˎ);
            }
            float f = this.ˊ.ˎ == 3 ? BarrierParameters.FRICTION : 1.0f;
            if (this.ˊ.ʼ != f) {
                this.ˊ.ʼ = f;
                this.ˊ.ˋ.ॱ(f);
            }
        }
    }

    public interface b {
        void ˏ(int i);

        void ॱ(float f);
    }

    public AudioFocusManager(@Nullable Context context, b bVar) {
        AudioManager audioManager;
        if (context == null) {
            audioManager = null;
        } else {
            audioManager = (AudioManager) context.getApplicationContext().getSystemService("audio");
        }
        this.ˊ = audioManager;
        this.ˋ = bVar;
        this.ॱ = new a();
        this.ˎ = 0;
    }

    public float ˊ() {
        return this.ʼ;
    }

    public int ॱ(boolean z) {
        if (this.ˊ == null) {
            return 1;
        }
        return z ? ˏ() : -1;
    }

    public int ˏ(boolean z, int i) {
        if (this.ˊ == null) {
            return 1;
        }
        if (z) {
            return i == 1 ? ˊ(z) : ˏ();
        } else {
            ॱ();
            return -1;
        }
    }

    public void ˋ() {
        if (this.ˊ != null) {
            ˏ(true);
        }
    }

    private int ˊ(boolean z) {
        return z ? 1 : -1;
    }

    private int ˏ() {
        int i = 0;
        if (this.ᐝ != 0) {
            if (this.ˎ == 0) {
                int ʽ;
                if (anr.ˋ >= 26) {
                    ʽ = ʽ();
                } else {
                    ʽ = ˎ();
                }
                if (ʽ == 1) {
                    ʽ = 1;
                } else {
                    ʽ = 0;
                }
                this.ˎ = ʽ;
            }
            if (this.ˎ == 0) {
                return -1;
            }
            if (this.ˎ != 2) {
                i = 1;
            }
            return i;
        } else if (this.ˎ == 0) {
            return 1;
        } else {
            ˏ(true);
            return 1;
        }
    }

    private void ॱ() {
        ˏ(false);
    }

    private void ˏ(boolean z) {
        if (this.ᐝ != 0 || this.ˎ != 0) {
            if (this.ᐝ != 1 || this.ˎ == -1 || z) {
                if (anr.ˋ >= 26) {
                    ʻ();
                } else {
                    ᐝ();
                }
                this.ˎ = 0;
            }
        }
    }

    private int ˎ() {
        return ((AudioManager) amm.ॱ(this.ˊ)).requestAudioFocus(this.ॱ, anr.ʻ(((aby) amm.ॱ(this.ˏ)).ˋ), this.ᐝ);
    }

    @RequiresApi(26)
    private int ʽ() {
        if (this.ʽ == null || this.ॱॱ) {
            this.ʽ = (this.ʽ == null ? new Builder(this.ᐝ) : new Builder(this.ʽ)).setAudioAttributes(((aby) amm.ॱ(this.ˏ)).ˏ()).setWillPauseWhenDucked(ॱॱ()).setOnAudioFocusChangeListener(this.ॱ).build();
            this.ॱॱ = false;
        }
        return ((AudioManager) amm.ॱ(this.ˊ)).requestAudioFocus(this.ʽ);
    }

    private void ᐝ() {
        ((AudioManager) amm.ॱ(this.ˊ)).abandonAudioFocus(this.ॱ);
    }

    @RequiresApi(26)
    private void ʻ() {
        if (this.ʽ != null) {
            ((AudioManager) amm.ॱ(this.ˊ)).abandonAudioFocusRequest(this.ʽ);
        }
    }

    private boolean ॱॱ() {
        return this.ˏ != null && this.ˏ.ॱ == 1;
    }
}
